//
//  TipCommentViewController.m
//  TipsProject
//
//  Created by Suny on 15/6/20.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "TipCommentViewController.h"
#import "CommentService.h"
#import "UserInfo.h"
#import "UITableView+Refresh.h"
#import "LYKeyBoardView.h"
#import "LYTextView.h"
#import "TipCommentDisplayInfo.h"

#define kNoCommentCoverImage        @"common_cover_nocomment.png"

@interface TipCommentViewController ()<UITableViewRefreshDelegate>
@property (nonatomic, strong) CommentService *service;
@property (nonatomic, strong) TipInfo *tipInfo;
@property (nonatomic, strong) LYKeyBoardView * keyBoard;
@property (nonatomic, strong) UIImageView *noCommentImageView;
@end
@implementation TipCommentViewController

- (UIImageView *)noCommentImageView
{
    if (!_noCommentImageView)
    {
        _noCommentImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _noCommentImageView.top = -100;
        _noCommentImageView.image = [UIImage imageNamed:kNoCommentCoverImage];
        _noCommentImageView.contentMode = UIViewContentModeCenter;
    }
    
    return _noCommentImageView;
}

- (CommentService *)service
{
	if (!_service)
	{
		_service = [[CommentService alloc] initDefaultBaseUrlString];
	}
	
	return _service;
}


- (id)initWithTipInfo:(TipInfo *)tipInfo
{
	self = [self init];
	if (self) 
	{
		self.tipInfo = tipInfo;
		self.hasNavigationBar = YES;
	}
	return self;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	
	[self setNavigationTitle:@"评论"];
	self.navigationItem.leftBarButtonItem = self.defaultBackButtonItem;
	[self.view addSubview:self.coverView];
	self.tableView.delegate = self;
	self.tableView.isOpenRefreshFun = YES;

	self.keyBoard = [[LYKeyBoardView alloc]
					 initDelegate:self
					 superView:self.view];
	self.keyBoard.inputText.placeholder = @"说两句！";

	[self.coverView startAnimating];
	
	[self requestDataFromNet];
}

- (void)doneLoadingTableViewData:(UITableView *)tableView
{
	self.pageIndex = 0;
	[self requestDataFromNet];
}

- (void)requestDataFromNet
{
	WEAKSELF
	[self.service getCommentListByTipId:self.tipInfo.tipId
							 pageNumber:self.pageIndex
							finishBlock:^(BaseResponse *response) {
								STRONGSELF
                                [strongSelf handleGetCommentListResponse:response];
							}];
}
- (void)doneLoadMoreData:(UITableView *)tableView
{
	self.pageIndex ++;
	[self requestDataFromNet];
}


- (void)handleGetCommentListResponse:(BaseResponse *)response
{
	[self.tableView  finishLoadingTableViewData];
    if (response.success)
    {
        [self.coverView stopAnimating];
		if (self.pageIndex == 0)
		{
			[self.modelArray removeAllObjects];
		}
        [self.modelArray addObjectsFromArray:response.object];
        
        if ([self.modelArray count] == 0)
        {
            [self.tableView addSubview:self.noCommentImageView];
        }
        else
        {
            [_noCommentImageView removeFromSuperview];
            _noCommentImageView = nil;
        }
        
        [self reloadTableView];
		
		if ([response.object count] >= kDefaultPageSize)
		{
			self.tableView.loadMoreStyle = UITableViewLoadMoreStyle1;
		}
		else
		{
			self.tableView.loadMoreStyle = UITableViewLoadMoreStyleNon;

		}

    }
    else
    {
        if (response.httpResponseCode == kNetConnectionCode)
        {
            if (self.modelArray.count == 0)
            {
                [self.coverView stopAnimationWithRetryAction:@selector(requestDataFromNet)
                                               withActObject:self];

            }
            else
            {
                [self.coverView stopAnimating];
            }
            [TipHandler showNewWorkNotReachable];

        }
        else
        {
            [self.coverView stopAnimating];
            [TipHandler showDataErrorTextOnly];
        }

    }

}

-(void)keyBoardView:(LYKeyBoardView*)keyBoard sendMessage:(NSString*)message
{
	if (CHECK_VALID_STRING(message))
    {
		[self publishComment:message];
	}
}

- (void)publishComment:(NSString *)content
{

    TipComment *comment = [[TipComment alloc] init];
	comment.commentContent = content;
	comment.commenterId = [LoginUserInfo sharedInstance].userId;
	comment.commentObjectId = self.tipInfo.tipId;
	comment.commentType = CommentTypeNomal;
	
	WEAKSELF
	[self.service publishComment:comment finishBlock:^(BaseResponse *response) {
		STRONGSELF
		[strongSelf handlePublishCommentResponse:response];
		
		
	}];
}


- (void)handlePublishCommentResponse:(BaseResponse *)response
{
	if (response.success)
	{
		[self.keyBoard tapAction];
		self.keyBoard.inputText.text = @"";
        [TipHandler showSuccess:@"评论成功"];

        
	}
	else
	{
		[self showErrorMessage:response.desc];
	}
}

- (void)dealloc
{
	self.tableView.isOpenRefreshFun = NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath withModelInfo:(NSObject *)modelInfo
{
	
	TipComment *comment  = (TipComment *)modelInfo;
	
	
	self.keyBoard.inputText.text = [NSString stringWithFormat:@"回复 %@ ：",comment.commenterNickName];
	
	[self.keyBoard.inputText becomeFirstResponder];
}

@end
